import Tooltip from 'uxcore-tooltip';

export default {

  defaults(props) {
    return {
      searchParams: {
        page: 0,
        size: 15,
        type: '',
        content: '',
        time: ''
      },
      tableOption: {
        size: 'small',
        pagerSizeOptions: [15, 30, 50, 100],
        showColumnPicker: false,
        showPager: true,
        showPagerTotal: true,
        pageSize: 15,
        showPagerSizeChanger: true,
        showHeaderBorder: true,
        showSearch: true,
        searchBarPlaceholder: '搜索消息内容',
        jsxdata: {
          data: []
        },
        jsxcolumns: [
          {
            dataKey: 'type', title: '消息类型', width: '15%', ordered: false,
            render: (type, message) => {
              switch (type) {
                case "JD":
                  return message.msglx;
                  break;
                case "YA":
                  return "扬尘黄色预警";
                  break;
                case "RA":
                  return "扬尘红色预警";
                  break;
                default:
                  return "未知";
                  break;
              }
            }
          },
          {
            dataKey: 'txms', title: '消息内容', width: '65%', ordered: false,
            render: function (cellData, rowData) {
              return (
                <Tooltip
                  placement="top"
                  overlay={
                    <div className="pop_ms">
                      <span>{`${rowData.glName}>>>${rowData.txms}`}</span>
                    </div>
                  }
                >
                  <div className="fn-text-overflow gc_title">
                    <span className="gc_title-click"
                          onClick={() => console.log(rowData)}>{`${rowData.glName}>>>${rowData.txms}`}</span>
                  </div>
                </Tooltip>
              )
            }
          },
          {dataKey: 'jssj', title: '接收时间', width: '20%', ordered: false}
        ]
      },
    }
  },

  // 获取历史消息
  async allMessage(ctx, props) {

    const content = await ctx.fn.DB.message.all({Authorization: ctx.getProps().token, ...props}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          tableOption: {
            jsxdata: {
              $set: content
            }
          }
        })
      );
    }
  },

  // 根据关键字查询消息
  async searchMessage(ctx, props) {
    const content = await ctx.fn.DB.message.search({Authorization: ctx.getProps().token, ...props}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          tableOption: {
            jsxdata: {
              $set: content
            }
          },
          searchParams: {$set: props}
        })
      );
    } else {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          tableOption: {
            jsxdata: {
              data: {$set: []},
              currentPage: {$set: 1},
              totalCount: {$set: 0}
            }
          },
          searchParams: {$set: props}
        })
      );
    }
  }

}
